package com.common.utils.executorsutils.pool;

import java.util.Random;
import java.util.concurrent.Callable;

/**
 * 扣减库存线程
 */
public class DeductionStoreThread implements Callable {

    private int number = 0;

    private Store store;


    public DeductionStoreThread(int number, Store store) {
        this.number = number;
        this.store = store;
    }

    public int getNumber() {
        return number;
    }

    /**
     * 扣减库存操作
     * @return 扣减库存数量
     */
    public synchronized int start(){
        Random random = new Random();
        //随机扣减库存数量
        int randomNumber = random.nextInt(120) + 1;
        number = randomNumber;
        boolean isTrue = store.increment(randomNumber);
        //扣减成功
        if(isTrue == true){
            return randomNumber;
        }else {
            return 0;
        }
    }

    @Override
    public Object call() throws Exception {
        int number = start();
        return number;
    }
}
